//关闭弹窗-layer
//复选事件
/*给单选框或复选框添加样式*/
function checkbox($this){
    if($this.attr('type')=="checkbox"){
        if ($this.attr('checked')=="checked") {
            $this.removeAttr("checked");
            $this.next('img').attr("src","/static/images/c_checkbox_off.png");
        }else{
            $this.attr("checked","checked");
            $this.next('img').attr("src","/static/images/c_checkbox_on.png");
        }
    }
}
function fmtDate(obj){
    var date =  new Date(obj);
    var y = 1900+date.getYear();
    var m = "0"+(date.getMonth()+1);
    var d = "0"+date.getDate();
    return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
}
function change_time(obj){
    var date =  new Date(obj);
    var y = 1900+date.getYear();
    var m = "0"+(date.getMonth()+1);
    var d = "0"+date.getDate();
    return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
}
var allId = []
$(function(){
    //全选
    $('.SelectAllImg').click(function(){
        var $list_input=$(this).parents('.tbhead').next('.tbbody-user').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".tbbody-user input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                allId=idAll
            })
            console.log(1)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            allId=[];
            console.log(2)
        }
        checkbox($(this));
    })
    //单选
    $("body").on("change",".tbbody-user input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.tbbody-user').prev('.tbhead').find('input');
        if($(this).parents('.tbhead').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".tbbody-user input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                allId=idAll
            })




        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  allId.indexOf(id);
            console.log(index)
            if(index>-1){
                allId.splice(index, 1);
            }else{
                allId=[]
            }
            console.log(allId)

        }
    })
})

//顶部导航菜单js
$(function(){


    $("#sample_error_1").on('click',function(){
        if(allId.length==0){
            layer.msg('请选择您要纠错的危化品',{icon:2,time:1500});
            return;
        }else if(allId.length>1){
            layer.msg('一次只能纠错一个危化品',{icon:2,time:1500});
            return;
        }else{
            var goods_id = allId['0']
            console.log(goods_id)
            location.href='/dangerous/dangerous/correction/'+goods_id;

        }
    })
})

$(function(){
    //历史记录-弹窗
    $('#sample_bussHist').on('click', function(){
        layer.open({
            type:1,
            title: '历史记录',
            area:['800px',''],
            btn:["确定","取消"],
            shadeClose:true,
            offset: '10px', //只设置顶部
            content:$('.j-bussHistModel'),
        });
    });

    $("#j-uploader input[type='file']").change(function(){
        $(this).parents().find(".j-buessuper").val($(this).val())
    })
    //变更


})

//筛选时间
$(function(){
    $(".cust_id input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var cust_id = $(this).val();
            console.log(cust_id)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/dangerous/dangerous/goods_id_screen",
                dataType: 'json',
                data: {cust_id: cust_id},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){

                        var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();

                        for (var i = 0; i < dataList.length; i++) {
                            if(dataList[i].alias!='') {
                                dataList[i].aliasAll = ''
                                var b = dataList[i].alias.split("，");

                                for (var j = 0; j < b.length; j++) {

                                    dataList[i].aliasAll += '<a class="oa">' + b[j] + '</a>'

                                }
                                console.log( dataList[i].aliasAll)
                            }else{

                                dataList[i].aliasAll = ''
                            }

                            var str = "";
                            str ='<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+dataList[i].id+'</td><td class="buss_td"><a href="/dangerous/dangerous/dangerous_details'+dataList[i].id+'">'+dataList[i].dangerous_id+'</a></td><td class="buss_td">'+dataList[i].xl_name+'</td><td class="buss_td">'+dataList[i].zh_name+'</td><td class="buss_td">'+dataList[i].molecule+'</td><td class="buss_td"><div class="controls wlft wei-othername">'+dataList[i].aliasAll+'</div> </td><td class="buss_td">'+dataList[i].cas+'</td></tr>'
                            tr.append(str);
                            console.log(str)
                        }

                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })
    $(".cust_xl_name input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var cust_xl_name = $(this).val();
            console.log(cust_xl_name)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/dangerous/dangerous/cust_xl_name_screen",
                dataType: 'json',
                data: {cust_xl_name: cust_xl_name},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){

                        var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();

                        for (var i = 0; i < dataList.length; i++) {
                            if(dataList[i].alias!='') {
                                dataList[i].aliasAll = ''
                                var b = dataList[i].alias.split("，");

                                for (var j = 0; j < b.length; j++) {

                                    dataList[i].aliasAll += '<a class="oa">' + b[j] + '</a>'

                                }
                                console.log( dataList[i].aliasAll)
                            }else{

                                dataList[i].aliasAll = ''
                            }

                            var str = "";
                            str ='<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+dataList[i].id+'</td><td class="buss_td"><a href="/dangerous/dangerous/dangerous_details'+dataList[i].id+'">'+dataList[i].dangerous_id+'</a></td><td class="buss_td">'+dataList[i].xl_name+'</td><td class="buss_td">'+dataList[i].zh_name+'</td><td class="buss_td">'+dataList[i].molecule+'</td><td class="buss_td"><div class="controls wlft wei-othername">'+dataList[i].aliasAll+'</div> </td><td class="buss_td">'+dataList[i].cas+'</td></tr>'
                            tr.append(str);
                            console.log(str)
                        }

                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })
    $(".credit_code input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var cust_zh_name = $(this).val();
            console.log(cust_zh_name)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/dangerous/dangerous/cust_zh_name_screen",
                dataType: 'json',
                data: {cust_zh_name: cust_zh_name},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){

                        var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();

                        for (var i = 0; i < dataList.length; i++) {
                            if(dataList[i].alias!='') {
                                dataList[i].aliasAll = ''
                                var b = dataList[i].alias.split("，");

                                for (var j = 0; j < b.length; j++) {

                                    dataList[i].aliasAll += '<a class="oa">' + b[j] + '</a>'

                                }
                                console.log( dataList[i].aliasAll)
                            }else{

                                dataList[i].aliasAll = ''
                            }

                            var str = "";
                            str ='<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+dataList[i].id+'</td><td class="buss_td"><a href="/dangerous/dangerous/dangerous_details'+dataList[i].id+'">'+dataList[i].dangerous_id+'</a></td><td class="buss_td">'+dataList[i].xl_name+'</td><td class="buss_td">'+dataList[i].zh_name+'</td><td class="buss_td">'+dataList[i].molecule+'</td><td class="buss_td"><div class="controls wlft wei-othername">'+dataList[i].aliasAll+'</div> </td><td class="buss_td">'+dataList[i].cas+'</td></tr>'
                            tr.append(str);
                            console.log(str)
                        }

                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })
    $(".legal_name_fenzi input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var legal_name_fenzi = $(this).val();
            console.log(legal_name_fenzi)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/dangerous/dangerous/legal_name_fenzi_screen",
                dataType: 'json',
                data: {legal_name_fenzi: legal_name_fenzi},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){

                        var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();

                        for (var i = 0; i < dataList.length; i++) {
                            if(dataList[i].alias!='') {
                                dataList[i].aliasAll = ''
                                var b = dataList[i].alias.split("，");

                                for (var j = 0; j < b.length; j++) {

                                    dataList[i].aliasAll += '<a class="oa">' + b[j] + '</a>'

                                }
                                console.log( dataList[i].aliasAll)
                            }else{

                                dataList[i].aliasAll = ''
                            }

                            var str = "";
                            str ='<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+dataList[i].id+'</td><td class="buss_td"><a href="/dangerous/dangerous/dangerous_details'+dataList[i].id+'">'+dataList[i].dangerous_id+'</a></td><td class="buss_td">'+dataList[i].xl_name+'</td><td class="buss_td">'+dataList[i].zh_name+'</td><td class="buss_td">'+dataList[i].molecule+'</td><td class="buss_td"><div class="controls wlft wei-othername">'+dataList[i].aliasAll+'</div> </td><td class="buss_td">'+dataList[i].cas+'</td></tr>'
                            tr.append(str);
                            console.log(str)
                        }

                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })
    $(".alias input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var alias = $(this).val();
            console.log(alias)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/dangerous/dangerous/alias_screen",
                dataType: 'json',
                data: {alias: alias},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){

                        var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();

                        for (var i = 0; i < dataList.length; i++) {
                            if(dataList[i].alias!='') {
                                dataList[i].aliasAll = ''
                                var b = dataList[i].alias.split("，");

                                for (var j = 0; j < b.length; j++) {

                                    dataList[i].aliasAll += '<a class="oa">' + b[j] + '</a>'

                                }
                                console.log( dataList[i].aliasAll)
                            }else{

                                dataList[i].aliasAll = ''
                            }

                            var str = "";
                            str ='<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+dataList[i].id+'</td><td class="buss_td"><a href="/dangerous/dangerous/dangerous_details'+dataList[i].id+'">'+dataList[i].dangerous_id+'</a></td><td class="buss_td">'+dataList[i].xl_name+'</td><td class="buss_td">'+dataList[i].zh_name+'</td><td class="buss_td">'+dataList[i].molecule+'</td><td class="buss_td"><div class="controls wlft wei-othername">'+dataList[i].aliasAll+'</div> </td><td class="buss_td">'+dataList[i].cas+'</td></tr>'
                            tr.append(str);
                            console.log(str)
                        }

                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })
    $(".cas input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var cas = $(this).val();
            console.log(cas)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/dangerous/dangerous/cas_screen",
                dataType: 'json',
                data: {cas: cas},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {

                    if(data.status==1){

                        var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();

                        for (var i = 0; i < dataList.length; i++) {
                            if(dataList[i].alias!='') {
                                dataList[i].aliasAll = ''
                                var b = dataList[i].alias.split("，");

                                for (var j = 0; j < b.length; j++) {

                                    dataList[i].aliasAll += '<a class="oa">' + b[j] + '</a>'

                                }
                                console.log( dataList[i].aliasAll)
                            }else{

                                dataList[i].aliasAll = ''
                            }

                            var str = "";
                            str ='<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+dataList[i].id+'</td><td class="buss_td"><a href="/dangerous/dangerous/dangerous_details'+dataList[i].id+'">'+dataList[i].dangerous_id+'</a></td><td class="buss_td">'+dataList[i].xl_name+'</td><td class="buss_td">'+dataList[i].zh_name+'</td><td class="buss_td">'+dataList[i].molecule+'</td><td class="buss_td"><div class="controls wlft wei-othername">'+dataList[i].aliasAll+'</div> </td><td class="buss_td">'+dataList[i].cas+'</td></tr>'
                            tr.append(str);
                            console.log(str)
                        }

                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })
})
$("#ImportPicInput").fileinput({
    uploadUrl: "/aptitude/license/upload",
    uploadAsync: true,
    showUpload:true,
    browseClass: "btn file-primary",
    // allowedFileExtensions: ['jpg', 'png', 'gif'],
    // maxFileSize
    uploadExtraData: function(previewId, index) {
        return {cust_id:allId['0']};
    },
    showPreview: false,
    showCaption: false,
    maxFileCount: 1
}).on('fileuploaded', function(e, params) {
    console.log(params)
    if(params.response.status==1){
        layer.msg(params.response.msg,{icon:1,time:1500});
    }else{
        layer.msg(params.response.msg,{icon:2,time:1500});
    }

});
